package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrMedDrawDAO;
import com.trkj.dao.jpa.IYzrMedDrawDetDAO;
import com.trkj.dao.jpa.IYzrPharmacyDAO;
import com.trkj.dao.jpa.IYzrStorehouseDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrMedDraw;
import com.trkj.pojos.YzrMedDrawDet;
import com.trkj.pojos.YzrPharmacy;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.vo.YzrMedDrawDetVo;
import com.trkj.vo.YzrMedDrawVo;

@Service
@Transactional
public class YzrDrawService {
	
	@Autowired
	private IYzrMedDrawDAO yzrMedDrawDAO;//提领单
	@Autowired
	private IYzrMedDrawDetDAO yzrMedDrawDetDAO;//提领详情单
	@Autowired
	private IYzrTbstaffDAO yzrTbstaffDAO;//员工
	@Autowired
	private IYzrPharmacyDAO yzrPharmacyDAO;	//药房
	@Autowired
	private IYzrStorehouseDAO yzrStorehouseDAO;//药库

	/**
	 * 查询出‘未提领’且审批‘已通过’的提领单
	 * @return
	 */
	public List<YzrMedDrawVo> queryDrawByDrawState(){
		return drawListToVo(yzrMedDrawDAO.findDrawByDrawState());
	}
	
	/**
	 * 审批提领单
	 * @param shenpiState
	 * @param drawId
	 */
	public void shenpiDraw(String shenpiState, String drawId){
		YzrMedDraw draw = yzrMedDrawDAO.findOne(drawId);
		draw.setMedDrawAutditState(shenpiState);
		draw.setMedDrawAutditTime(new Date());
		yzrMedDrawDAO.save(draw);
	}
	
	/**
	 * 根据当然登录用户查出由该用户审批的提领单
	 * @param page
	 * @param rows
	 * @param staffId
	 * @return
	 */
	public Map<String, Object> queryDrawByStaffId(int page, int rows, String staffId){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<YzrMedDraw> list = yzrMedDrawDAO.findDrawByStaffId(new BigDecimal(staffId), pr);
		Map<String, Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", drawListToVo(list));
		return map;
	}
	
	
	/**
	 * 根据时间段查询
	 * @param startDate
	 * @param endDate
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> queryByDrawDate(Date startDate,Date endDate,int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		
		if(startDate.getTime() > endDate.getTime()){
			Date t = startDate;
			startDate = endDate;
			endDate = t;
		}
		List<YzrMedDraw> list = yzrMedDrawDAO.findDrawByTime(startDate, endDate, pr);
		Map<String, Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", drawListToVo(list));		
		return map;
	}
	
	/**
	 * 根据不同条件查询提领信息
	 * @param page
	 * @param rows
	 * @param name
	 * @param value
	 * @return
	 */
	public Map<String, Object> queryByIf(int page, int rows,String name,String value){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<YzrMedDraw> list = new ArrayList<>();

		if(name.equals("zdr")){
			List<Tbstaff> staff = yzrTbstaffDAO.queryByStaffName(value);
			if(staff.size() > 0){
				for(Tbstaff s: staff){
					list = yzrMedDrawDAO.findByZdr(new BigDecimal(s.getStaffNo()), pr);
				}
			}					
		}else if(name.equals("spr")){
			List<Tbstaff> staff = yzrTbstaffDAO.queryByStaffName(value);
			if(staff.size() > 0){
				for(Tbstaff s: staff){
					list = yzrMedDrawDAO.findBySpr(new BigDecimal(s.getStaffNo()), pr);
				}
			}					
		}else if(name.equals("tlr")){
			List<Tbstaff> staff = yzrTbstaffDAO.queryByStaffName(value);
			if(staff.size() > 0){
				for(Tbstaff s: staff){
					list = yzrMedDrawDAO.findByTlr(new BigDecimal(s.getStaffNo()), pr);
				}
			}					
		}		
		else if(name.equals("spzt")){
			list = yzrMedDrawDAO.findBySpzt(value, pr);
		}else if(name.equals("tlzt")){
			list = yzrMedDrawDAO.findByTlzt(value, pr);
		}else if(name.equals("drawId")){
			list = yzrMedDrawDAO.findByDraw(value, pr);
		}	
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", drawListToVo(list));
		
		return map;
	}
	/**
	 * 根据提领id查看提领单
	 * @param medDrawId
	 * @return
	 */
	public YzrMedDrawVo queryDrawById(String medDrawId){
		return drawPoToVo(yzrMedDrawDAO.findOne(medDrawId));
	}
	
	/**
	 * 根据提领id查看提领详情单
	 * @param page
	 * @param rows
	 * @param medDrawId
	 * @return
	 */
	public  Map<String, Object> queryDrawDetById(int page, int rows, String medDrawId){
		PageRequest pr = new PageRequest(page - 1, rows);		
		List<YzrMedDrawDet> list = yzrMedDrawDetDAO.findDrawDetByDrawId(medDrawId, pr);	
		Map<String, Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", list);
		return map;
	}
	
	
	
	/**
	 * 提交提领单的数据到数据库
	 * @param storeList
	 * @param zdr
	 * @param spr
	 * @param stored
	 * @param pharmcy
	 */
	public void addMedDraw(YzrMedDrawDetVo medDrawList, String zdr, String spr, String stored, String pharmacy){

		String year = getNowDate();
		//拼接
		String name = "TL-" + year;	//提领
		String nameDet = "TD-" + year;//提领详情

		String n = yzrMedDrawDAO.findMaxId(name);
		//生成id
		YzrEntityService entityService = new YzrEntityService();
		String drawId = entityService.getId(name, n);
		
		///提领号，制定人，审核人，审核时间，提领人，提领时间，被提领仓库，提领药房，审核状态，是否提领
		System.err.println("制定人："+zdr);
		YzrMedDraw draw = new YzrMedDraw();
		draw.setMedDrawId(drawId);
		draw.setMedDrawPickPerson(new BigDecimal(zdr));
		draw.setMedDrawAutdit(new BigDecimal(spr));
		draw.setMedDrawStoreId(stored);
		draw.setMedDrawPharmacyId(pharmacy);
		draw.setMedDrawAutditState("待审批");
		draw.setMedDrawDrawState("未提领");
		yzrMedDrawDAO.save(draw);
		
		List<YzrMedDrawDet> drawDetList = medDrawList.getMedDrawList();
		if(drawDetList.size() > 0){
			for(YzrMedDrawDet drawDet: drawDetList){
				//药品编号，药品名，批号，单位，规格，数量
				String nDet = yzrMedDrawDetDAO.findMaxId(nameDet);
				drawDet.setMedDrawDetId(entityService.getId(nameDet, nDet));
				drawDet.setDrawDetDrawId(drawId);
				yzrMedDrawDetDAO.save(drawDet);
			}
		}
		
	}

	
	/**
	 * 分页查询提领单
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> queryAllDraw(int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);		
		List<YzrMedDraw> list = yzrMedDrawDAO.findAllDraw(pr);
		long tatol = yzrMedDrawDAO.count();
		Map<String, Object> map = new HashMap<>();
		map.put("total", tatol);
		map.put("rows", drawListToVo(list));
		return map;
	}	
	private List<YzrMedDrawVo> drawListToVo(List<YzrMedDraw> list){
		if(list == null) return null;
		List<YzrMedDrawVo> listVo = new ArrayList<>();
		
		for(YzrMedDraw purchDet: list){
			YzrMedDrawVo purchDetVo = drawPoToVo(purchDet);
			listVo.add(purchDetVo);
		}
		return listVo;
	}	
	public YzrMedDrawVo drawPoToVo(YzrMedDraw purchDet){
		YzrMedDrawVo purchDetVo = new YzrMedDrawVo();
		purchDetVo.setMedDrawId(purchDet.getMedDrawId());
		purchDetVo.setMedDrawPickPerson(purchDet.getMedDrawPickPerson());
		purchDetVo.setMedDrawAutdit(purchDet.getMedDrawAutdit());
		purchDetVo.setMedDrawAutditState(purchDet.getMedDrawAutditState());
		purchDetVo.setMedDrawAutditTime(purchDet.getMedDrawAutditTime());
		purchDetVo.setMedDrawDrawPerson(purchDet.getMedDrawDrawPerson());
		purchDetVo.setMedDrawDrawState(purchDet.getMedDrawDrawState());
		purchDetVo.setMedDrawDrawTime(purchDet.getMedDrawDrawTime());
		purchDetVo.setMedDrawPharmacyId(purchDet.getMedDrawPharmacyId());
		purchDetVo.setMedDrawStoreId(purchDet.getMedDrawStoreId());
	
		BigDecimal staffZd = purchDet.getMedDrawPickPerson();
		BigDecimal staffSp = purchDet.getMedDrawAutdit();
		BigDecimal staffTl = purchDet.getMedDrawDrawPerson();
		
		String storeName = purchDet.getMedDrawStoreId();
		String pharmacy = purchDet.getMedDrawPharmacyId();
		
		if(staffZd != null){		
			long sId = staffZd.longValue();		
			Tbstaff staff = yzrTbstaffDAO.findOne(sId);	
			purchDetVo.setStaffNameZd(staff==null?null:staff.getStaffName());
		}
		if(staffSp != null){		
			long sId = staffSp.longValue();		
			Tbstaff staff = yzrTbstaffDAO.findOne(sId);	
			purchDetVo.setStaffNameSp(staff==null?null:staff.getStaffName());
		}
		if(staffTl != null){		
			long sId = staffTl.longValue();		
			Tbstaff staff = yzrTbstaffDAO.findOne(sId);	
			purchDetVo.setStaffNameTl(staff==null?null:staff.getStaffName());
		}
		if(pharmacy != null){			
			YzrPharmacy phar = yzrPharmacyDAO.findOne(pharmacy);
			purchDetVo.setStoreName(phar==null?null:phar.getPharmacyName());
		}
		if(storeName != null){			
			YzrStorehouse store = yzrStorehouseDAO.findOne(storeName);
			purchDetVo.setPharmacyName(store==null?null:store.getStoreName());
		}
		return purchDetVo;
	}
	
	/**
	 * 格式化日期
	 * @return
	 */
	public static String getNowDate() {
		  Date currentTime = new Date();
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		  String dateString = formatter.format(currentTime);
		  return dateString;
	}
}
